﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace WoBian.Common
{
    /// <summary>
    /// 通用方法
    /// </summary>
    public static class CommonFunction
    {
        public static string ToNullableString(this object aNullabelObject)
        {
            if (aNullabelObject == null)
                return string.Empty;
            return aNullabelObject.ToString();
        }

        public static string ToMd5(this string aStr)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] b = md5.ComputeHash(UnicodeEncoding.UTF8.GetBytes(aStr));
            return BitConverter.ToString(b).Replace("-",string.Empty).ToLower();            
        }

        public static int ToInt32(this object aObject)
        {
            return Convert.ToInt32(aObject);
        }

        public static double ToDdouble(this object aObject)
        {
            return Convert.ToDouble(aObject);
        }

        public static decimal ToDecimal(this object aObject)
        {
            return Convert.ToDecimal(aObject);
        }

        public static bool ToBoolean(this object aObject)
        {
            return Convert.ToBoolean(aObject);
        }

        public static DateTime? ToDateTime(this object aObject)
        {
            if (!Convert.IsDBNull(aObject)&&aObject!=null)
            {
                return Convert.ToDateTime(aObject);
            }
            return null;
        }

        public static byte[] ToBytes(this object aObject)
        {
            return Encoding.Default.GetBytes(aObject.ToNullableString());
        }
    }
}
